package com.vnt;


import java.io.IOException;

import javax.microedition.lcdui.Image;

import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.util.Metrics;
import org.kalmeo.kuix.widget.Picture;
import org.kalmeo.kuix.widget.PopupBox;
import org.kalmeo.kuix.widget.Screen;
import org.kalmeo.kuix.widget.ScrollPane;
import org.kalmeo.kuix.widget.Text;
import org.kalmeo.kuix.widget.Widget;
import org.kalmeo.kuix.widget.Screen.ScreenMenu;
import org.kalmeo.util.frame.Frame;
import com.ilove.core.*;
import com.ilove.frame.*;
public class ContentFrame implements Frame {

	// static instance of MainFrame
	public static ContentFrame instance = new ContentFrame();
	public int id;// 0 text data from vas, 1 ringtone, 2 game ,3 wallpaper
	public String contentId;
	public String contentName;
	public String avatar;
	public PopupBox pop;
	public int currPage = 1;
	public int totalPage = 1;
	public String[] vecContent = null;
//	private String closeDivTag = "</div>";
	boolean isShort = false;
	// MainFrame's Screen
	private Screen screen;

	// The Pad that hold all shopLists
	private final Pad pad = new Pad();
	public int groupId;

	/**
	 * Show the screen
	 */
	public void showScreen() {
		if (screen != null) {
			screen.setCurrent();
		}
	}
	public void onAdded() {
		try {
			System.gc();
			System.out.println("Content Frame on add " +id+ " " +contentId+ " " +contentName);
			prepareContent();
			currPage = 1;
			
			// Open the Pad
			pad.setTitle(contentName);
			// Preload the screen
			
			screen = Kuix.loadScreen(ILOVEContants.CONTENT_SCREEN, pad);
			screen.setCurrent();
			
			// initial currPage by 1
			if(id ==0 || id==4 || id==5)
			{
				changePageValue(currPage);
			}
			else
			{	
				System.out.println("avatar"+avatar);
				Picture pictureavatar = (Picture)screen.getWidget("avatar");
				pictureavatar.setImage(pad.getImage(avatar));
			}
			// Draw page area
			int screenWidth = Kuix.getCanvas().getWidth();
			ScreenMenu rightMenu = (ScreenMenu)screen.getSecondMenu();
			Metrics metrics = rightMenu.getPreferredSize(screenWidth);
			int containerWidth = screenWidth - (2*metrics.width)-10;
			Widget container = screen.getWidget("CTM_PAGE");
			container.setAttribute("style", "layout:flowlayout(true, fill);align:center;min-size:"+containerWidth + " 16");
		} catch (Exception e) {
			Kuix.alert(e);
			e.printStackTrace();
		}
	}
	/* (non-Javadoc)
	 * @see org.kalmeo.util.frame.Frame#onMessage(java.lang.Object, java.lang.Object[])
	 */
	public boolean onMessage(Object identifier, Object[] arguments) {
		if ("askQuit".equals(identifier)) {
			// show askQuit box wich call onMessage method with "quit" name and null arguments
			Kuix.alert(	Kuix.getMessage("ASK_QUIT"),
					KuixConstants.ALERT_QUESTION | KuixConstants.ALERT_YES | KuixConstants.ALERT_NO,
					"!exit", 
					null);
			return false;
		} else if ("back".equals(identifier)) {
			Kuix.getFrameHandler().removeFrame(instance);
			return false;
		} else if ("about".equals(identifier)) {
			Kuix.getFrameHandler().pushFrame(AboutFrame.instance);
			return false;
		} else if ("arrLeft".equals(identifier)) {
			if (currPage > 1) currPage--;
			changePageValue(currPage);
		} else if ("arrRight".equals(identifier)) {
			if (currPage < totalPage) currPage++;
			changePageValue(currPage);
		}
		return true;
	}

	public void changePageValue(int page) {
		Text textPage = (Text)screen.getWidget("textPage");
		textPage.setText(page + "/" + totalPage);
		System.out.println("changePageValue");
		Picture pictureLeft = (Picture)screen.getWidget("pic-left");
		Picture pictureRight = (Picture)screen.getWidget("pic-right");
		
		try {
			if (page == 1) {
				pictureLeft.setImage(Image.createImage("/img/left-dis.png"));
			} else {
				pictureLeft.setImage(Image.createImage("/img/left.png"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (page == totalPage) {
				pictureRight.setImage(Image.createImage("/img/right-dis.png"));
			} else {
				pictureRight.setImage(Image.createImage("/img/right.png"));
			}
			
			ScrollPane scrollPane = (ScrollPane)screen.getWidget("SP");
			scrollPane.setHorizontal(false);
			System.out.println(vecContent[page-1]);
			pad.setContent((String)vecContent[page-1]);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void prepareContent() {
		try {
			//
			ContentList content = pad.getContentData(contentId,id,contentName);
			if(id ==0  || id== 4 || id==5)
			{
				String[] arrContent = pad.split(content.data, "\n");
				String data = pad.proccessData(arrContent, 320);
				vecContent = pad.split(data, "@@@");
			}
			else if(id == 1)
			{
				avatar  = "http://210.211.99.58:6868/Upload"+content.data;
			}
			if (vecContent != null) 
				totalPage = vecContent.length;
			else 
				totalPage = 0;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/* (non-Javadoc)
	 * @see org.kalmeo.util.frame.Frame#onRemoved()
	 */
	public void onRemoved() {
		// show MainFrame screen
		try {
			if(id==4)
//				com.ilove.lottery.frame.LotteryMainFrame.instance.showScreen();
				com.vnt.IndexFrame.instance.showScreen();
			else
			SubIndexFrame.instance.showScreen();
			screen.cleanUp();
			screen = null;
		} catch (Exception e) {
		}
	}

}
